package robotCross;


import java.awt.Component;

import javax.swing.JLabel;

import com.supermap.desktop.implement.SmMenu;
import com.supermap.desktop.implement.SmMenuItem;

import abbot.finder.matchers.AbstractMatcher;
import abbot.util.AWT;

/** Provides matching of Components by component text. */
public class TextMatcher extends AbstractMatcher {
    private String text;

    /** Construct a matcher that will match any component that has
        explicitly been assigned the given <code>text</code>.  Auto-generated
        texts (e.g. <code>win0</code>, <code>frame3</code>, etc. for AWT
        (native) based components will not match.
    */
    public TextMatcher(String text) {
        this.text = text;
    }

    /** @return whether the given component has been explicitly assigned the
        text given in the constructor.
    */
    public boolean matches(Component c) {
    	String ctext = "";
    	if(c instanceof JLabel){
    		ctext = ((JLabel) c).getText();
    	}
    	else if(c instanceof SmMenu){
    		ctext = ((SmMenu) c).getText();
    	}
    	else if(c instanceof SmMenuItem){
    		ctext = ((SmMenuItem) c).getText();
    	}
        if (text == null)
            return ctext == null || AWT.hasDefaultName(c);
        return stringsMatch(text, ctext);
    }
    public String toString() {
        return "Text matcher (" + text + ")";
    }
}
